﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using SignalR;
using SignalR.Hubs;

namespace SignalRHubs
{
    public class MapHub :  Hub 
    {
        // List of connected Phone clients on server .. feel free to persist or use this list however.
        private static List<PhoneClient> PhoneClientList = new List<PhoneClient>();        

        // SignalR method call to add a new Phone client connection & map pushpin.
        public void JoinFromPhone(string phoneID, float latitude, float longitude)
        {
            PhoneClient phoneClientToAdd = new PhoneClient();
            phoneClientToAdd.PhoneClientId = phoneID;            
            phoneClientToAdd.Latitude = latitude;
            phoneClientToAdd.Longitude = longitude;

            PhoneClientList.Add(phoneClientToAdd);

            // Map phone client.
            Clients.addClient(phoneClientToAdd);
            this.Caller.addClients(PhoneClientList.ToArray());          
        }

        // Disconnect given Phone client & unmap pushpin.
        public void Disconnect(string phoneID)
        {
            PhoneClient client = null;

            foreach (PhoneClient existingClient in PhoneClientList)
            {
                if (phoneID == existingClient.PhoneClientId)
                {
                    client = existingClient;
                    break;
                }
            }

            // Cleanup & remove from map.
            PhoneClientList.Remove(client);
            Clients.removeClient(client);            
        }
    }
}